﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MongoDB.Bson;
using MongoDB.Driver;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Xml.Linq;

namespace BattleShip_MongoDB
{
    public partial class LogIn : Form
    {
        public LogIn()
        {
            InitializeComponent();
        }

        private String strUserName = "";
        private String strPassword = "";

/////////////////////////////////// *** Button LogIn Click *** /////////////////////////////////////

        private void buttonLogin_Click(object sender, EventArgs e)
        {
            strUserName = textUserName.Text.ToString();
            strPassword = textPassword.Text.ToString();
            labelStatus.Text = "";

            // Connect to MongoDB
            MongoServer mongoDB = MongoServer.Create();
            mongoDB.Connect();

            var db = mongoDB.GetDatabase("BattleShipDataBase");

            using (mongoDB.RequestStart(db))
            {
                var collection = db.GetCollection<BsonDocument>("Users");
                var query = new QueryDocument("UserName", strUserName);

                foreach (BsonDocument item in collection.Find(query))
                {
                    BsonElement Password = item.GetElement("Password");

                    if (Password.Value.Equals(strPassword))
                    {
                        labelStatus.Text = "Đăng nhập thành công";

                        UserInformation.UserID = item.GetElement("UserID").Value;
                        UserInformation.UserName = (string)item.GetElement("UserName").Value;
                        UserInformation.UserPass = (string)item.GetElement("Password").Value;
                        UserInformation.NumberOfWin = (string)item.GetElement("NumberOfWin").Value;
                        UserInformation.NumberOfLose = (string)item.GetElement("NumberOfLost").Value;

                        if (UserInformation.gameRoom == null)
                            UserInformation.gameRoom = new GameRoom();
                        UserInformation.gameRoom.Show();
                        this.Hide();
                    }
                }
            }

            // End Connect to MongoDB
            mongoDB.Disconnect();
           
            if (labelStatus.Text.Equals(""))
            {
                labelStatus.Text = "Đăng nhập thất bại!\nUser name hoặc Password không đúng.";
            }
        }    


/////////////////////////////////// *** Button LogIn Click *** /////////////////////////////////////

        private void buttonSignIn_Click(object sender, System.EventArgs e)
        {
            CreateAccount frame = new CreateAccount();
            frame.Show();
            this.Hide();
        }
         
    }
}